package com.kattomy.ssh.action;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;

import com.kattomy.ssh.entities.Employee;
import com.kattomy.ssh.service.DepartmentService;
import com.kattomy.ssh.service.EmployeeService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class EmployeeAction extends ActionSupport implements RequestAware, 
	ModelDriven<Employee>, Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private EmployeeService employeeService;
	private DepartmentService departmentService;
	private Integer id;
	
	public void setId(Integer id) {
		this.id = id;
	}
	
	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}
	
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}
	
	public String index() {
		System.out.println("...");
		return "index";
	}
	public void prepareIndex() {
		if (id != null) {
			model = employeeService.getEmployee(id);
		}
		System.out.println("...");
	}
	
	public String list(){
		request.put("employees", employeeService.getAllEmployee());
		return "list";
	}

	private InputStream inputStream;
	
    public InputStream getInputStream() {
        return inputStream;
    }
	
	public String delete() {
		try {
			employeeService.deleteEmployee(id);
			inputStream = new ByteArrayInputStream("1".getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			try {
				inputStream = new ByteArrayInputStream("0".getBytes("UTF-8"));
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
		return "ajax-success";
	}
	
	public String input(){
		request.put("departments", departmentService.getAllDepartment());
//		List<Department> dl = new ArrayList<Department>();
//		dl.add(new Department(1, "人事部"));
//		dl.add(new Department(2, "研发部"));
//		dl.add(new Department(3, "销售部"));
//		request.put("departments", dl);
		return INPUT;
	}
	
	public void prepareInput() {
		if (id != null) {
			model = employeeService.getEmployee(id);
		}
	}
	
	public String save(){
		if (id == null) {
			model.setCreateTime(new Date());
		}
//		model.setBirth(new Date());
		employeeService.saveOrUpdateEmployee(model);
		return SUCCESS;
	}
	
	public void prepareSave() {
		if (id == null) {
			model = new Employee();
		}else {
			model = employeeService.getEmployee(id);
		}
	}
	
	private String lastName;
	
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
	public String validateLastName() throws UnsupportedEncodingException {
		if (employeeService.validForLastName(lastName)) {
			inputStream = new ByteArrayInputStream("1".getBytes("UTF-8"));
		}else {
			inputStream = new ByteArrayInputStream("0".getBytes("UTF-8"));
		}
		
		return "ajax-success";
	}

	private Map<String, Object> request;
	
	@Override
	public void setRequest(Map<String, Object> arg0) {
		this.request = arg0;
	}

	@Override
	public void prepare() throws Exception {}

	private Employee model;
	
	@Override
	public Employee getModel() {
		return model;
	}

}
